package dao.impl;

import dao.BaseDao;
import dao.Dorm_roomsDao;
import entity.Dorm_rooms;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Dorm_roomsDaoimpl extends BaseDao implements Dorm_roomsDao {

    @Override
    public List<Dorm_rooms> rooms(int id, String roomnumber, int floor) throws SQLException, ClassNotFoundException {
        getConnection();
        PreparedStatement pre = null;
        ResultSet ss = null;
        List<Dorm_rooms> list = new ArrayList<>();

        try {

            String sql = "SELECT capacity FROM dorm_rooms WHERE building_id = ? AND room_number = ? AND floor = ?";
            pre = conn.prepareStatement(sql);
            pre.setInt(1, id);
            pre.setString(2, roomnumber);
            pre.setInt(3, floor);
            ss = pre.executeQuery();

            while (ss.next()) {
                Dorm_rooms dor = new Dorm_rooms();
                dor.setCapacity(ss.getInt("capacity"));
                list.add(dor);
            }
            return list;
        } finally {
            if (ss != null) ss.close();
            if (pre != null) pre.close();
            if (conn != null) conn.close();
        }
    }

    @Override
    public int fjrs(String roomnumber, int floor, int buildingid) throws SQLException, ClassNotFoundException {
       getConnection();
        PreparedStatement pre = null;
        ResultSet re = null;

        try {

            String sql = "SELECT capacity FROM dorm_rooms WHERE room_number = ? AND floor = ? AND building_id = ?";
            pre = conn.prepareStatement(sql);
            pre.setString(1, roomnumber);
            pre.setInt(2, floor);
            pre.setInt(3, buildingid);
            re = pre.executeQuery();

            return re.next() ? re.getInt(1) : 0;
        } finally {
            if (re != null) re.close();
            if (pre != null) pre.close();
            if (conn != null) conn.close();
        }
    }
}